package com.zrrd.gmall.list.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.zrrd.gmall.common.result.Result;
import com.zrrd.gmall.list.service.SearchService;
import com.zrrd.gmall.model.list.Goods;
import com.zrrd.gmall.model.list.SearchParam;
import com.zrrd.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/api/list")
public class ListApiController {
    //注入es对象
    @Autowired
    private ElasticsearchRestTemplate restTemplate;
    @Autowired
    private SearchService searchService;
    //仅用了两行就创建了索引库
    @GetMapping("/inner/createIndex")
    public Result createIndex(){
        //创建索引库
        restTemplate.createIndex(Goods.class);
        //映射
        restTemplate.putMapping(Goods.class);
        return Result.ok();
    }
    //上架商品
    @GetMapping("/inner/upperGoods/{skuId}")
    public Result upperGood(@PathVariable("skuId")Long skuId){
        searchService.upperGoods(skuId);
        return Result.ok();
    }
    //下架商品
    @GetMapping("/inner/lowerGoods/{skuId}")
    public Result lowerGood(@PathVariable("skuId")Long skuId){
        searchService.lowerGoods(skuId);
        return Result.ok();
    }
    //更新热度
    @GetMapping("/inner/incrHotScore/{skuId}")
    public Result incrHotScore(@PathVariable("skuId") Long skuId){
        searchService.incrHotScore(skuId);
        return Result.ok();
    }

    //搜索商品
    @PostMapping
    public Result list(@RequestBody SearchParam searchParam) throws IOException {
        SearchResponseVo responseVo = searchService.search(searchParam);
        return Result.ok(responseVo);
    }
}
